package com.pro.springbootshop.service.impl;

import com.pro.springbootshop.entity.Seller;
import com.pro.springbootshop.entity.Wares;
import com.pro.springbootshop.entityExtend.Search;
import com.pro.springbootshop.entityExtend.SearchReturn;
import com.pro.springbootshop.mapper.MainClassesMapper;
import com.pro.springbootshop.mapper.OtherClassesMapper;
import com.pro.springbootshop.mapper.SellerMapper;
import com.pro.springbootshop.mapper.WaresMapper;
import com.pro.springbootshop.service.WaresService;
import com.pro.springbootshop.util.SellerTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Service
public class WaresServiceImpl implements WaresService {
    public WaresMapper waresMapper;
    @Autowired
    public void setUserMapper (WaresMapper waresMapper) {
        this.waresMapper = waresMapper;
    }
    public OtherClassesMapper otherClassesMapper;
    @Autowired
    public void setUserMapper (OtherClassesMapper otherClassesMapper) {
        this.otherClassesMapper = otherClassesMapper;
    }
    public MainClassesMapper mainClassesMapper;
    @Autowired
    public void setUserMapper (MainClassesMapper mainClassesMapper) {
        this.mainClassesMapper = mainClassesMapper;
    }
    @Autowired
    SellerTokenUtil sellerTokenUtil;
    public SellerMapper sellerMapper;
    @Autowired
    public void setUserMapper (SellerMapper sellerMapper) {
        this.sellerMapper = sellerMapper;
    }

    @Override
    public SearchReturn findWares(Search search,String token) {
        int current;
        int size;
        int mainClassId;
        int waresState;
        int otherClassId;
        String sellerId;
        if(token!=null){
            Map<String,String> sellerMap=sellerTokenUtil.parseToken(token);
            sellerId=sellerMap.get("sellerId");
            Seller seller=sellerMapper.sellerSearch(sellerId);
            if(seller==null){
                sellerId="admin";
            }
        }else {
            sellerId="admin";
        }

        String value;
        if(search.getMainClassId()==null){
            mainClassId=0;
        }else{
            mainClassId=search.getMainClassId();
        }
        if(search.getOtherClassId()==null){
            otherClassId=0;
        }else {
            otherClassId=search.getOtherClassId();
        }
        if(search.getPageNo()==null){
            current=0;
        }else {
            current=(search.getPageNo()-1)*search.getPageSize();
        }
        if(search.getPageSize()==null){
            size=10;
        }else {
            size=search.getPageSize();
        }
        if(search.getKeyword()==null){
            value="%%";
        }else {
            value='%'+search.getKeyword()+'%';
        }
        if(search.getWaresState()==null){
            waresState=0;
        }else {
            waresState= search.getWaresState();
        }
        List<Wares> waresList= waresMapper.findWares(current, size, mainClassId, otherClassId, value,waresState,sellerId,"admin");
        for(Wares wares:waresList){
            wares.setPictureList(wares.getWaresPicture().split(";"));
        }
        int total=waresMapper.findWaresTotals( mainClassId, otherClassId, value,waresState,sellerId,"admin").size();
        return new SearchReturn(waresList,total,size, current+1, (total+size-1)/size);
    }

    @Override
    public Wares getWares(Integer waresId) {
        Wares wares = waresMapper.selectById(waresId);
        wares.setPictureList(wares.getWaresPicture().split(";"));
        wares.setMainClasses(mainClassesMapper.selectById(wares.getMainClassId()));
        wares.setOtherClasses(otherClassesMapper.selectById(wares.getOtherClassId()));
        return wares;
    }

    @Override
    public String deleteWares(Integer waresId) {
        int i = waresMapper.deleteById(waresId);
        if(i>0){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }

    @Override
    public String insertWares(Wares wares) {
        wares.setWaresState(1);
        int i =waresMapper.insert(wares);
        if(i>0){
            return "上架成功";
        }else{
            return "上架失败";
        }
    }

    @Override
    public String updateWares(Wares wares) {
        int i =waresMapper.updateById(wares);
        if(i>0){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }

    @Override
    public String upload(MultipartFile file, String sign, String path) throws IOException {
//        System.out.println("文件大小:"+file.getSize());
//        System.out.println(file.getOriginalFilename());
//        System.out.println(path);
        //动态生成服务器的存储目录
        saveFile(file,path,sign);
//        System.out.println(sign);
//      return "http://120.55.64.65:5050/upload/"+sign+file.getOriginalFilename();
       return "http://127.0.0.1:5050/upload/"+sign+file.getOriginalFilename();
    }
    public void saveFile(MultipartFile file,String path,String sign)throws IOException{
        File dir = new File(path);
        if(!dir.exists()){
            dir.mkdir();
        }
        String newPath=path+'/';
        File newDir = new File(newPath);
        if(!newDir.exists()){
            newDir.mkdir();
        }
        File newFile=new File(newPath+sign+file.getOriginalFilename());
        file.transferTo(newFile);
    }
}
